package com.wfk.service.impl;

import com.wfk.entity.OrderGoods;
import com.wfk.mapper.OrderGoodsMapper;
import com.wfk.service.IOrderGoodsService;
import com.wfk.utils.BasePage;
import com.wfk.utils.ValidateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 积分购买记录 服务实现类
 * </p>
 *
 * @since 2019-01-13
 */
@Service
public class OrderGoodsServiceImpl extends ServiceImpl<OrderGoodsMapper, OrderGoods> implements IOrderGoodsService {

	@Override
	public BasePage<OrderGoods> getList(BasePage<OrderGoods> page, OrderGoods orderGoods) {
		QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<OrderGoods>();
		queryWrapper.orderByDesc("id");
		if(orderGoods.getCid()!=null) {
			queryWrapper.eq("cid",orderGoods.getCid());
		}
		if(ValidateUtil.date(orderGoods.getStart())) {
			queryWrapper.gt("createtime",orderGoods.getStart());
		}
		if(ValidateUtil.date(orderGoods.getEnd())) {
			queryWrapper.lt("createtime",orderGoods.getEnd());
		}
		if(ValidateUtil.empty(orderGoods.getOrderid())) {
			queryWrapper.eq("orderid",orderGoods.getOrderid());
		}
		if(!ValidateUtil.objEmpty(orderGoods.getStatus())) {
			queryWrapper.eq("status",orderGoods.getStatus());
		}
		if(!ValidateUtil.objEmpty(orderGoods.getPaytype())) {
			queryWrapper.eq("paytype",orderGoods.getPaytype());
		}
		if(ValidateUtil.date(orderGoods.getStart())) {
			queryWrapper.ge("createtime",orderGoods.getStart());
		}
		if(ValidateUtil.date(orderGoods.getEnd())) {
			queryWrapper.le("createtime",orderGoods.getEnd());
		}
		if(page.isPage()) {
			super.page(page, queryWrapper);
		}else {
			List<OrderGoods> list = super.list(queryWrapper);
			page.setRecords(list);
		}
		return page;
	}

	@Override
	public BigDecimal moneyCount(Integer status, Date time) {
		QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<OrderGoods>();
		queryWrapper.select("IFNULL(SUM(paymoney),0.00) AS moneyCount");
		if(time!=null) {
			queryWrapper.eq("DATE_FORMAT(createtime,'%m-%d-%Y')", "DATE_FORMAT("+time+",'%m-%d-%Y')");
		}
		if(status!=null) {
			queryWrapper.gt("status",status);
		}
		Map<String, Object> map = super.getMap(queryWrapper);
		return (BigDecimal) map.get("moneyCount");
	}

	@Override
	public OrderGoods getOrderid(String orderid) {
		if(!ValidateUtil.empty(orderid)) {
			return null;
		}
		QueryWrapper<OrderGoods> queryWrapper = new QueryWrapper<OrderGoods>();
		queryWrapper.eq("orderid", orderid);
		return super.getOne(queryWrapper);
	}


}
